<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/3/9
 * Time: 11:15
 */

namespace app\restful\controller;
use app\restful\model\Article as ArticleModel;
use app\restful\code\ErrorCode;
use app\restful\controller\Member;
use think\Error;
use think\Exception;
//文章的增删查改
class Article
{
    /**
     * @var
     */
   // private $_article;

    //通过构造方法对对象属性进行初始化赋值
   // public function __construct($_article)
    //{
     //   $this->_article = $_article;
    //}


    /**
     * 文章发布
     * @param $title
     * @param $content
     * @param $user_id
     * @return array
     * @throws Exception
     */
    public function create($title,$content,$user_id){
       if(empty($title)){
           throw new Exception("文章标题不能为空",ErrorCode::ARTICLE_TITLE_CANNOT_EMPTY);
       }
        if(empty($content)){
            throw new Exception("文章内容不能为空",ErrorCode::ARTICLE_CONTENT_CANNOT_EMPTY);
        }
        $res = ArticleModel::create(['title'=>$title,'content'=>$content,'user_id'=>$user_id]);
        if(!$res){
            throw new Exception('文章发布失败',ErrorCode::ARTICLE_PUBLISH_FAIL);
        }
        return [
            'title' => $title,
            'content' => $content
        ];
    }


    /**
     * 文章编辑
     * @param $article_id
     * @param $title
     * @param $content
     * @param $user_id
     * @return array
     * @throws Exception
     */
    public function edit($article_id,$title,$content,$user_id){
         //权限
        if(empty($article_id)){
            throw new Exception("请求参数错误",ErrorCode::REQUEST_PARAMS_ERROR);
        }
        $arr = $this->view($article_id);
        if($arr['user_id'] != $user_id){
            throw new Exception("你无权编辑",ErrorCode::NO_AUTH);
        }
        $title = empty($title)?$arr['title']:$title;
        $content = empty($content)?$arr['content']:$content;
        if($title === $arr['title'] && $content === $arr['content']){
            return $arr;
        }
        $data = ['title'=>$title,'content'=>$content];
        $res = ArticleModel::update($data,['Id'=>$article_id]);
        return [
            'title'=>$res['title'],
            'content' =>$res['content']
        ];

    }

    /**
     * 文章列表
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getList(){
        $article = new ArticleModel();
        $arr = $article->db()->field("*")->select();
        return $arr;
    }

    //单个文章显示
    public function view($article_id){

        $article = new ArticleModel();
       $res = $article->db()->field("*")->where("Id",$article_id)->find();

        if(!$res){
            throw  new Exception("没有该文章",ErrorCode::ARTICLE_NULL);
        }
        return [
            'title'=>$res['title'],
            'content' =>$res['content'],
            'article_id' => $res['Id'],
            'user_id'   => $res['user_id']
        ];

    }


    //文章删除
    public function delete($article_id,$user_id){
       $arr = $this->view($article_id);
        if($arr['user_id'] != $user_id){
            throw new Exception("你没有权限",ErrorCode::NO_AUTH);
        }
       $res =  ArticleModel::destroy($article_id);
        if(!$res){
            throw new Exception("删除失败",ErrorCode::ARTICLE_DELETE_ERROR);

        }
        return true;
    }



















}